package DataCrawler;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;

import java.util.regex.*;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class CidCrawler implements PageProcessor {
    public String resDir;
    public String url;
    public String userAgent;
    public String cookie;
    public String output = "out";
    private Site site = Site.me().setRetryTimes(5).setSleepTime(1000).setCharset("UTF-8")
            .setUserAgent(userAgent);

    public CidCrawler setUrl(String url) {
        this.url = url;
        return this;
    }
    
    public CidCrawler setResources(String res) {
        this.resDir = res;
        return this;
    }

    public CidCrawler setUserAgent(String userAgent) {
        this.userAgent = userAgent;
        return this;
    }
    
    public CidCrawler setCookie(String cookie) {
        this.cookie = cookie;
        return this;
    }

    public CidCrawler setPageOutputFile(String filenameNoExtension) {
        output = filenameNoExtension;
        return this;
    }

    public void run() {
        System.out.println("[CR]: 开始运行。。。");
        Spider.create(this).addUrl(url).thread(1).run();
        System.out.println("[CR]: 结束，结果保存到了" + resDir + output + "。");

    }

    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
        page.putField("head", page.getHtml().xpath("/html/head"));
        String text = page.getResultItems().get("head").toString();

        // Data processing
        Pattern pt = Pattern.compile("<script>window.__INITIAL_STATE__.*?</script>");
        Pattern pt2 = Pattern.compile("\\{.+\\};");
        Matcher mch = pt.matcher(text);
        try {
            FileWriter writer = new FileWriter(new File(resDir + output +".json"), Charset.forName("utf8"));
            FileWriter html = new FileWriter(new File(resDir + output + ".html"), Charset.forName("utf8"));

            html.write(text);
            html.flush();
            html.close();

            if (mch.find()) {
                String s = mch.group();
                mch = pt2.matcher(s);
                if (mch.find()) {
                    s = mch.group();
                    writer.write(s.replace(';', ' '));
                }
                else {
                    // 
                    System.out.println("[CR re]找不到");
                }
            }
            else {
                System.out.println("[CR re]找不到");
            }
            writer.flush();
            writer.close();
        } catch (IOException e) {
            System.err.println(e.toString());
        }
    }

    public static void main(String[] args) {
        var c = new CidCrawler();
        c
        .setCookie("buvid3=E37F2A73-7A88-07C5-B7DD-7CCE71B1759F53472infoc; i-wanna-go-back=-1; _uuid=AE8B103DA-10B7B-89CD-FD10F-244567937D7E57589infoc; buvid4=221FD661-E3D9-2C03-18F1-32E950E8F25158734-022040109-NSMokBUPpjNXtme2Loy40g==; buvid_fp_plain=undefined; CURRENT_BLACKGAP=0; LIVE_BUVID=AUTO5916489524695224; blackside_state=0; rpdid=|(u))RJmYkml0J'uYR)mlkYl); nostalgia_conf=-1; hit-dyn-v2=1; DedeUserID=25301637; DedeUserID__ckMd5=5006c283b8d931f2; b_ut=5; CURRENT_QUALITY=64; PVID=1; fingerprint=a9b75dcddbeb6531c36f67ae0456f411; SESSDATA=1112f040,1668256819,e6456*51; bili_jct=ac8bd39b877a623cfd41b62a7d8efec8; sid=815mt0po; buvid_fp=a9b75dcddbeb6531c36f67ae0456f411; bp_video_offset_25301637=661024367726035000; CURRENT_FNVAL=4048; innersign=1; b_lsid=33B3822B_180D0DC9D41")
        .setUrl("https://www.bilibili.com/video/BV1H44y1T74v?spm_id_from=333.1007.top_right_bar_window_default_collection.content.click")
        .setResources("src\\main\\resources\\")
        .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47")
        .setPageOutputFile("Page")
        .run();
    }
}
